<?php

namespace tmcore\services\notice\applet;

use EasyWeChat\MiniApp\Application;
use tmcore\exceptions\WeChatException;
use tmcore\services\wechat\WxConfigService;

class AppletTemplateService
{
    protected Application $app;

    protected array $config;

    /**
     * 初始化
     */
    public function __construct()
    {
        $this->config = $this->getConfig();
        $this->app = new Application($this->config);
    }

    /**
     * 获取配置
     * @return array
     * @throws WeChatException
     */
    protected function getConfig()
    {
        $config = WxConfigService::getMnpConfig();
        if (empty($config['app_id']) || empty($config['secret'])) {
            throw new WeChatException('请先设置微信小程序配置');
        }
        return $config;
    }


    /**
     * 获取小程序模板库类目列表
     * @return \EasyWeChat\Kernel\HttpClient\Response|\Symfony\Contracts\HttpClient\ResponseInterface
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
     */
    public function getCategoryList() {
        return $this->app->getClient()->get('wxaapi/newtmpl/getcategory');
    }


    /**
     * 获取模板库某个模板标题下关键词库
     * @param $id
     * @return \EasyWeChat\Kernel\HttpClient\Response|\Symfony\Contracts\HttpClient\ResponseInterface
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
     */
    public function getKeyWord($id) {
        return $this->app->getClient()->postJson('wxaapi/newtmpl/getpubtemplatekeywords', [
            'tid' => $id
        ]);
    }


    /**
     * 组合模板并添加至帐号下的个人模板库
     * @param $id
     * @param $keywordIdList
     * @return \EasyWeChat\Kernel\HttpClient\Response|\Symfony\Contracts\HttpClient\ResponseInterface
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
     */
    public function addTemplate($id, $keywordIdList) {
        return $this->app->getClient()->postJson('wxaapi/newtmpl/addtemplate', [
            'tid' => $id,
            'kidList' => $keywordIdList
        ]);
    }


    /**
     * 获取帐号下已存在的模板列表
     * @return \EasyWeChat\Kernel\HttpClient\Response|\Symfony\Contracts\HttpClient\ResponseInterface
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
     */
    public function getTemplateList() {
        return $this->app->getClient()->get('wxaapi/newtmpl/gettemplate');
    }


    /**
     * 删除帐号下的某个模板
     * @param $templateId
     * @return \EasyWeChat\Kernel\HttpClient\Response|\Symfony\Contracts\HttpClient\ResponseInterface
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
     */
    public function deleteTemplate($templateId) {
        return $this->app->getClient()->postJson('wxaapi/newtmpl/deltemplate', ['priTmplId' => $templateId]);
    }


    /**
     * 发送模板消息
     * @param $params
     * @return \EasyWeChat\Kernel\HttpClient\Response|\Symfony\Contracts\HttpClient\ResponseInterface
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
     */
    public function sendTemplate($params) {
        return $this->app->getClient()->postJson('cgi-bin/message/subscribe/send', [
            'touser' => $params['openid'],
            'template_id' => $params['template_id'],
            'page' => $params['page'],
            'form_id' => $params['form_id'],
            'data' => $params['data'],
        ]);
    }
}